{extend name='./layout/_content_open'}

{block name='content_open'}
<div class="login layui-anim layui-anim-up">
    <div class="message"><strong>TP</strong>管理登录</div>
    <div id="darkbannerwrap"></div>
    <form action="#" method="POST" class="form-horizontal" role="form" id="login-form">
        <div class="form-group">
            <div class="col-sm-12">
                <input type="text" class="form-control" id="username" name="username"
                       placeholder="输入用户名" required data-bv-notempty-message="用户名不为空"
                       pattern="^.{2,10}$" data-bv-regexp-message="用户名在两到十个字符之间" value=""/>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-12">
                <input type="password" class="form-control" id="password" name="password"
                       placeholder="输入密码" required data-bv-notempty-message="密码不为空"
                       pattern="^.{1,20}$" data-bv-regexp-message="密码在一到二十个字符之间" value=""/>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-12 text-right">
                <div class="checkbox">
                    <label>
                        <input type="checkbox" value="1" name="rememberMe" id="rememberMe">记住我
                    </label>
                </div>
            </div>
        </div>
    </form>
    <button ele-range type="button" class="btn btn-block btn-success" onclick="view.login()"
            id="btn-login">
        登 录
    </button>
</div>
<script type="text/javascript">
    $(function () {
        view.init();
    });
    var view = {
        init: function () {
            if ($.cookie('username')) {
                $('#username').val($.cookie('username'));
            }
            if ($.cookie('password')) {
                $('#password').val($.cookie('password'));
                $('#rememberMe').attr('checked', 'checked');
            }

            $('#login-form').bootstrapValidator({
                message: '值不能为空',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                }
            });
            $('#login-form').on('submit', function () {
                return false;
            });
        },
        login: function () {
            $('#login-form').data('bootstrapValidator').validate();
            var b = $('#login-form').data('bootstrapValidator').isValid();
            if (!b) {
                return false;
            }
            $('#btn-login').attr('disabled', 'disabled');
            $('#a_login').attr('disabled', 'disabled');
            $.ajax({
                url: "{:url('/sys/login/login')}",
                type: 'post',
                dataType: 'json',
                data: $('#login-form').serialize(),
                success: function (data) {
                    if (data.status) {
                        if ($('input[type=checkbox][name=rememberMe]:checked').val() === '1') {
                            $.cookie('username', $('#username').val(), {expires: 7});
                            $.cookie('password', $('#password').val(), {expires: 7});
                        } else {
                            $.cookie('username', '');
                            $.cookie('password', '');
                        }
                        layer.msg(data.msg, {icon: 1});
                        wpgaci.goUrl("{:url('/sys/index/index')}", 2)
                    } else {
                        $('#btn-login').removeAttr('disabled');
                        $('#a_login').removeAttr('disabled');
                        layer.msg(data.msg, {icon: 2});
                    }
                },
                error: function () {
                    $('#btn-login').removeAttr('disabled');
                    $('#a_login').removeAttr('disabled');
                    layer.msg('网络错误，请稍后再试', {icon: 2});
                }
            });
        }
    };
</script>
{/block}